const KoaRouter = require('koa-router')
const HttpClient = require('../util/HttpClient')
const Template = require('../util/Template')

const router = new KoaRouter();
const modules = require('../../modules');

router.get('/task', async ctx => {
  let script = ['/task.list.js'];
  let modConfig = modules['task.list'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/task', 'get', null, {headers: reqHeaders});
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('工单列表', body, script, modConfig.template, ctx)
});

router
  .get('/task/edit', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/edit/:id', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/edit4CallCenter', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/copyTask', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/planTask/edit', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/planTask/create', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/planTask/create', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/planTask/edit', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/planTask/copy', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/planTask/view', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/event/convent2Task/jump', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/createFromCustomer/*', async ctx => {
    await taskEditContent(ctx);
  })
  .get('/task/createFromProduct/*', async ctx => {
    await taskEditContent(ctx);
  });
  
router.get('/task/view/:id', async ctx => {
  let modConfig = modules['task.view'];
  let reqHeaders = ctx.request.headers;
  let script = ['/task.view.js'];

  let url = `/task/view/${ctx.params.id}`;
  let result = await HttpClient.request(url, 'get', null, {headers: reqHeaders});
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('工单详情', body, script, modConfig.template, ctx)
});

async function taskEditContent(ctx) {
  // 临时：需要新旧工单编辑页面切换现在的代码和下面的注释代码
  await taskEditAllotContent(ctx)
  // let modConfig = modules['task.edit'];
  // let script = ['/task.edit.js'];

  // let { url, headers} = ctx.request;
  // let result = await HttpClient.request(url, 'get', null, { headers });
  // let body = result.body;
  
  // ctx.body = await Template.renderWithHtml('', body, script, modConfig.template, ctx)
}


const taskListRoute = {
  'close': '工单关闭',
  'allot': '工单指派',
  'review': '工单回访',
  'taskPool': '工单池'
}

for (const key in taskListRoute) {
  router.get(`/task/${key}`, async ctx => {
    let modConfig = modules[`task.${key}`];
    let script = [`/task.${key}.js`];
    
    let reqHeaders = ctx.request.headers;
    let result = await HttpClient.request(`/task/${key}`, 'get', null, {headers: reqHeaders});
    let body = result.body;

    ctx.body = await Template.renderWithHtml(`工单${taskListRoute[key]}列表`, body, script, modConfig.template, ctx)
  });
}

router.get('/task/planTask/list', async ctx => {
  let modConfig = modules['task.planTask.list']
  let script = ['/task.planTask.list.js']

  let reqHeaders = ctx.request.headers
  let result = await HttpClient.request('/task/planTask/list', 'get', null, { headers: reqHeaders })
  let body = result.body

  ctx.body = await Template.renderWithHtml('工单计划任务列表', body, script, modConfig.template, ctx)
})

router.get('/task/planTask/view', async ctx => {
  let modConfig = modules['task.planTask.view']
  let script = ['/task.planTask.view.js']

  let reqHeaders = ctx.request.headers
  let result = await HttpClient.request('/task/planTask/view', 'get', null, { headers: reqHeaders })
  let body = result.body

  ctx.body = await Template.renderWithHtml('工单计划任务详情', body, script, modConfig.template, ctx)
})

router.get('/balance/alreadySettlement', async ctx => {
  let modConfig = modules['task.balance.list']
  let script = ['/task.balance.list.js']

  let reqHeaders = ctx.request.headers
  let result = await HttpClient.request('/balance/alreadySettlement', 'get', null, { headers: reqHeaders })
  let body = result.body

  ctx.body = await Template.renderWithHtml('工单审核结算列表', body, script, modConfig.template, ctx)
})

async function taskEditAllotContent(ctx) {
  let modConfig = modules['task.edit.allot'];
  let script = ['/task.edit.allot.js'];
  
  let { url, headers} = ctx.request;
  let result = await HttpClient.request(url, 'get', null, { headers });
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('', body, script, modConfig.template, ctx)
}

module.exports = router;